package com.his.struts.action;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.google.gson.Gson;
import com.his.struts.dao.cook.CookDao;
import com.his.struts.dao.cook.CookImp;
import com.his.struts.dao.foods.FoodsDao;
import com.his.struts.dao.foods.FoodsImp;
import com.his.struts.form.CommentForm;
import com.his.struts.form.CookForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.PageUtil;

/**
 * 食谱类
 * 
 * @author mlxnle
 * 
 */
public class CookAction extends DispatchAction {
	CookDao cookDao = new CookImp();
	Logger log = Logger.getLogger(CookAction.class);
	/**
	 * 添加食谱
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addCook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward;
		CookForm cookForm = (CookForm) form;
		if (null != cookForm.getUpsmallurl() && null != cookForm.getCookname()) {
			String smallurl = cookForm.getUpsmallurl().getFileName();
			// 图片还没得到
			String imgurl = request.getRealPath("images/cook/");
			cookForm.setSmallurl("images/cook/" + smallurl);
			try {
				InputStream input = cookForm.getUpsmallurl().getInputStream();
				OutputStream output = new FileOutputStream(imgurl + "\\"
						+ smallurl);
				IOUtils.copy(input, output);
				input.close();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			cookDao.addCook(cookForm);
			String sql = "foodcook order by cid asc";
			// 调用分页类 
			ResultSet rs = PageUtil.getPage(sql, 20, request);
			// 如果数据不为空 
			if(rs != null){
				// 封装
				ArrayList<CookForm> allcoocList = cookDao.getAllCook(rs);
				// 数据放到request中
				request.setAttribute("shipuList", allcoocList);
			}
		}
		request.setAttribute("message", "添加成功");
		forward = mapping.findForward("bgcook");
		return forward;
	}

	/**
	 * 显示食谱列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showCooklist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ArrayList<CookForm> coocList = new ArrayList<CookForm>();
		coocList = cookDao.getCookList();
		AjaxUtil.send(response,coocList);
		return null;
	}

	/**
	 * 后台显示所有食谱
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showAllCook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sql = "foodcook order by cid asc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 20, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			ArrayList<CookForm> allcoocList = cookDao.getAllCook(rs);
			// 数据放到request中
			request.setAttribute("shipuList", allcoocList);
		}
		return mapping.findForward("bgcook");
	}

	/**
	 * 待添加的功能
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delCook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward = null;
		
		String cid = request.getParameter("cid");
		int ckid;
		if (null != cid) {
			ckid = Integer.parseInt(cid);
		} else {
			return null;
		}
		boolean ok = cookDao.deleteCook(ckid);
		if (ok) {
			String sql = "foodcook order by cid asc";
			// 调用分页类 
			ResultSet rs = PageUtil.getPage(sql, 20, request);
			// 如果数据不为空 
			if(rs != null){
				// 封装
				ArrayList<CookForm> allcoocList = cookDao.getAllCook(rs);
				// 数据放到request中
				request.setAttribute("shipuList", allcoocList);
			}
		}
		request.setAttribute("message", "删除成功");
		forward = mapping.findForward("bgcook");
		return forward;
	}

	/**
	 * 变比食谱
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editCook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward = null;
		CookForm cookForm = (CookForm) form;
		String imgurl = request.getRealPath("images/cook/");
		String cookname = cookForm.getCookname();
		int cid = cookForm.getCid();
		if (null != cookname) {
			cookDao.updateCook("cookname", cookname, cid);
		}
		if (cookForm.getUpsmallurl().getFileSize() != 0) {
			String smallurl = cookForm.getUpsmallurl().getFileName();
			// 图片还没得到
			cookForm.setSmallurl("images/cook/" + smallurl);
			try {
				InputStream input = cookForm.getUpsmallurl().getInputStream();
				OutputStream output = new FileOutputStream(imgurl + "\\"
						+ smallurl);
				IOUtils.copy(input, output);
				input.close();
				output.close();
			} catch (IOException e) {
				log.error(e.getMessage());
			}
			cookDao.updateCook("smallurl", "images/cook/" + smallurl, cid);
		}
		String sql = "foodcook order by cid asc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 20, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			ArrayList<CookForm> allcoocList = cookDao.getAllCook(rs);
			// 数据放到request中
			request.setAttribute("shipuList", allcoocList);
		}
		request.setAttribute("message", "修改成功");
		forward = mapping.findForward("bgcook");
		return forward;
	}

}